#!/bin/bash

set -x
set -e

function download_and_load()
{
    BASE_URL=$1
    MINIO_TARGET_PATH=$2
    FILENAME_LIST="${@:3}"
    for FILENAME in $FILENAME_LIST
    do
        FILE_URL=$BASE_URL/$FILENAME
        LOCAL_FILE=/tmp/$FILENAME
        TARGET_FILE=$MINIO_TARGET_PATH/$FILENAME
        curl -L -o $LOCAL_FILE $FILE_URL && mc cp $LOCAL_FILE $TARGET_FILE && rm $LOCAL_FILE
    done
}

RESOURCE_SERVICE=${MINIO_BASE_URL:-https://resource.static.zjvis.net/application/aiworks/minio}
mc config host add minio http://$MINIO_SERVER_HOST:$MINIO_SERVER_PORT $MINIO_SERVER_ACCESS_KEY $MINIO_SERVER_SECRET_KEY
mc mb --ignore-existing minio/model-online
download_and_load $RESOURCE_SERVICE/model-online/mo_21 minio/model-online/mo_21 \
    "my_model_params.json" "tsne_am.py"
download_and_load $RESOURCE_SERVICE/model-online/mo_22 minio/model-online/mo_22 \
    "my_model_params.json" "lle_am.py"
download_and_load $RESOURCE_SERVICE/model-online/mo_51 minio/model-online/mo_51 \
    "my_model_params.json" "arima_am.py"
download_and_load $RESOURCE_SERVICE/model-online/mo_52 minio/model-online/mo_52 \
    "my_model_params.json" "holtWinters_am.py"
download_and_load $RESOURCE_SERVICE/model-online/mo_53 minio/model-online/mo_53 \
    "my_model_params.json" "adf_am.py"
download_and_load $RESOURCE_SERVICE/model-online/mo_54 minio/model-online/mo_54 \
    "my_model_params.json" "ecg.py"

mc mb --ignore-existing minio/fasttext-model
download_and_load $RESOURCE_SERVICE/fasttext-model minio/fasttext-model \
    "autojoin" "mapping"

mc mb --ignore-existing minio/vis-platform
download_and_load $RESOURCE_SERVICE/vis-platform minio/vis-platform \
    "GeoLite2-City.mmdb"
mc mb --ignore-existing minio/graph-file
mc mb --ignore-existing minio/nebula
